// pages/report/report.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkbox:['虚假宣传','网络暴力','淫秽粗俗','扰乱秩序','违反法律'],
    pageID:'',
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user')
    var send = false
    if(!user){
      wx.setStorageSync('user', [])
    }

    var x
    for (x in user){
      if (options.id == user[x][0] && options.creator == user[x][1] && options.tourist == user[x][2]){
        var send = true
      }
    }

    if (send){
      wx.showModal({
        title: '提示',
        content: '你已经在本页面举报过该用户了，无需重复举报',
        showCancel:false,
        success:res=>{
      // 使用navigateBack就不会导致openid的流失，不必重新加载页面
          wx.navigateBack({
            url: '/pages/details/details?id=' + options.id +'&tourist='+options.tourist,
          })
        }
      })
    }
    this.data.pageID = options.id
    this.data.type = options.type
    this.data.tourist = options.tourist
    this.data.creator = options.creator
    this.data.content = options.content
  },

  submitForm(e){
    wx.showLoading({
      title: '正在发送',
    })
    var reportor = wx.getStorageSync('openID')
    if (e.detail.value.reportReason!=""){
      wx.cloud.callFunction({
        name: 'sendEmail',
        data: {
          pageID: this.data.pageID,
          content: e.detail.value,
          type: this.data.type,
          tourist:this.data.tourist,
          creator: this.data.creator,
          description:this.data.content,
          operation:'report',
          reportor: reportor
        },
        success:res=>{
          wx.hideLoading()
          wx.showToast({
            title: '感谢您的监督',
          })
          var user = wx.getStorageSync('user')
          user.push([this.data.pageID,this.data.creator,this.data.tourist])
          console.log('submit',this.data.pageID, this.data.creator, this.data.tourist)
          wx.setStorageSync('user', user)
          wx.navigateBack({
            url: '/pages/details/details?id=' + this.data.pageID,
          })
        }
      })
    }
    else{
      wx.showToast({
        title: '请选择举报原因',
        image:'/images/close-circle.png'
      })
    }
  }
})